package yto.freemarker.springbootstudy.config;

import ch.qos.logback.core.joran.spi.HostClassAndPropertyDouble;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author dongxing
 * @create 2018-02-26 14:08
 **/
//@Component
public class RedisConfig {

    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;

    @Bean
    public JedisCluster jedisCluster() {
        //分割节点
        String[] split = clusterNodes.replaceAll("，", ",").split(",");
        //创建set集合
        Set<HostAndPort> nodes = new HashSet<>();
        for (String node : split) {
            String[] hp = node.replaceAll("：", ":").split(":");
            nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));

        }
        //创建集群的
        JedisCluster jedisCluster = new JedisCluster(nodes);
        return jedisCluster;
    }
}
